package cn.appsys.controller.back;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sun.istack.internal.logging.Logger;

import cn.appsys.constant.Constants;
import cn.appsys.pojo.BackendUser;
import cn.appsys.service.back.user.BackUserService;

@Controller
@RequestMapping("/manager") 
public class BackLoginController {
	
	private Logger logger = Logger.getLogger(BackLoginController.class);
	
	@Resource
	private BackUserService backUserService;
	
	//注销
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constants.USER_SESSION);
		return "backendlogin";
	}
	
	//跳转登录
	@RequestMapping("/login")
	public String toLogin(){
		return "backendlogin";
	}
	
	//用于处理登录
	@RequestMapping("/doLogin")
	public String doLogin(@RequestParam("userCode") String userCode,
		         @RequestParam("userPassword")String userPassword,
							Model model,HttpSession session) {
		BackendUser backendUser = backUserService.login(userCode);
		logger.info("哈哈哈================"+userCode);
		if(backendUser == null){
			//没有查询到该用户信息
			model.addAttribute("error","用户名不存在");
			return "backendlogin";
		}else if (!backendUser.getUserPassword().equals(userPassword)) {
			//usercode查询到的用户密码和页面的密码不一致
			model.addAttribute("error", "密码输入错误");
			return "backendlogin";
		}else {
			//登录成功，将用户保存到session会话中
			session.setAttribute(Constants.USER_SESSION, backendUser);
			return "backend/main";
		}
	}
	
}